package com.kjlink.internal.workreport.plan.service;

import cn.xuqiudong.common.base.service.BaseService;
import com.kjlink.internal.workreport.plan.lookup.ToAddLookup;
import com.kjlink.internal.workreport.plan.mapper.WorkPlanMapper;
import com.kjlink.internal.workreport.plan.model.WorkPlan;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.user.service.UserService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 功能: :周工作计划 Service
 *
 * @author Vic.xu
 * @since 2024-10-10 09:56
 */
@Service
public class WorkPlanService extends BaseService<WorkPlanMapper, WorkPlan> {

    @Resource
    private UserService userService;

    @Override
    protected boolean hasAttachment() {
        return false;
    }

    /**
     * 根据用户id和开始结束日期查询
     *
     * @return
     */
    public WorkPlan findIfExist(ToAddLookup lookup) {
        return mapper.findIfExist(lookup);
    }

    /**
     * 根据用户id和开始结束日期查询 查询上一周工作计划主表
     */
    public WorkPlan findLastWeek(ToAddLookup lookup) {
        Date weekStart = DateUtils.addDays(lookup.getWeekStart(), -7);
        Date weekEnd = DateUtils.addDays(lookup.getWeekEnd(), -7);
        return mapper.findIfExist(new ToAddLookup(lookup.getUserId(), weekStart, weekEnd));
    }

    /**
     * 前往新增详情页面前先保存计划主表
     */
    public WorkPlan saveBeforeAddDetail(ToAddLookup lookup) {
        WorkPlan entity = new WorkPlan(lookup);
        User user = userService.findById(entity.getUserId());
        if (user != null) {
            entity.setGroupId(user.getGroupId());
        }
        this.save(entity);
        return findById(entity.getId());
    }
}
